﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DHISInteroperability.Timeframe
{
    public class PeriodFormatText
    {
        /// <summary>
        /// Interval:       Day
        /// Format:         yyyyMMdd
        /// Example:        20040315
        /// Description:    March 15 2004
        /// </summary>
        public const string DAILY = "Daily";

        /// <summary>
        /// Interval:       Week
        /// Format:         yyyyWn
        /// Example:        2004W10
        /// Description:    Week 10 2004
        /// </summary>
        public const string WEEKLY = "Weekly";

        /// <summary>
        /// Interval:       Month
        /// Format:         yyyyMM
        /// Example:        200403
        /// Description:    March 2004
        /// </summary>
        public const string MONTHLY = "Monthly";

        /// <summary>
        /// Interval:       Quarter
        /// Format:         yyyyQn
        /// Example:        2004Q1
        /// Description:    January-March 2004
        /// </summary>
        public const string QUARTERLY = "Quarterly";

        /// <summary>
        /// Interval:       Six-month
        /// Format:         yyyySn
        /// Example:        2004S1
        /// Description:    January-June 2004
        /// </summary>
        public const string SIX_MONTHLY = "SixMonthly";

        /// <summary>
        /// Interval:       Six-month April
        /// Format:         yyyyAprilSn
        /// Example:        2004AprilS1
        /// Description:    April-September 2004
        /// </summary>
        public const string SIX_MONTHLY_APRIL = "SixMonthlyApril";

        /// <summary>
        /// Interval:       Year
        /// Format:         yyyy
        /// Example:        2004
        /// Description:    2004
        /// </summary>
        public const string YEARLY = "Yearly";

        /// <summary>
        /// Interval:       Financial Year April
        /// Format:         yyyyApril
        /// Example:        2004April
        /// Description:    Apr 2004-Mar 2005
        /// </summary>
        public const string FINANCIAL_APRIL = "FinancialApril";

        /// <summary>
        /// Interval:       Financial Year July
        /// Format:         yyyyJuly
        /// Example:        2004July
        /// Description:    July 2004-June 2005
        /// </summary>
        public const string FINANCIAL_JULY = "FinancialJuly";

        /// <summary>
        /// Interval:       Financial Year Oct
        /// Format:         yyyyOct
        /// Example:        2004Oct
        /// Description:    Oct 2004-Sep 2005
        /// </summary>
        public const string FINANCIAL_OCT = "FinancialOct";
    }

    internal class StringValueAttribute : Attribute
    {
        private string _value;
        public StringValueAttribute(string value)
        {
            this._value = value;
        }

        public string Value
        {
            get { return _value; }
        }

        private static Hashtable _stringValues = new Hashtable();
        public static string GetStringValue(Enum value)
        {
            string output = null;
            Type type = value.GetType();

            // Check first in our cached results...
            if (_stringValues.ContainsKey(value))
            {
                output = (_stringValues[value] as StringValueAttribute).Value;
            }
            else
            {
                // Look for our 'StringValueAttribute'
                // in the field's custom attributes
                FieldInfo fieldInfo = type.GetField(value.ToString());
                StringValueAttribute[] attributes = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];

                if (attributes.Length > 0)
                {
                    _stringValues.Add(value, attributes[0]);
                    output = attributes[0].Value;
                }
            }

            return output;
        }
    }
}
